-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathwebpack.config.js
More file actions
124 lines (112 loc) · 3.69 KB
/
Copy pathwebpack.config.js
File metadata and controls
124 lines (112 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const babelLoader = require('@skpm/builder/lib/utils/babelLoader').default({})
const PRODUCTION = process.env.NODE_ENV !== 'development'
const OUTPUT_PATH = path.resolve(__dirname, './build')
const ENTRIES = [
{ entry: './Source/index.js', output: 'SketchAPI.js' },
{ entry: './Source/async/index.js', output: 'SketchAPI_async.js' },
{
entry: './Source/data-supplier/index.js',
output: 'SketchAPI_data-supplier.js',
},
{ entry: './Source/dom/index.js', output: 'SketchAPI_dom.js' },
{ entry: './Source/settings/index.js', output: 'SketchAPI_settings.js' },
{ entry: './Source/ui/index.js', output: 'SketchAPI_ui.js' },
]
const CORE_MODULES = Object.keys(
require('./core-modules/package.json').dependencies
).map((k) => k.replace('@skpm/', ''))
babelLoader.test = /\.(ts|js)$/
// This plugin generates the input .xcfilelist for the "Build SketchAPI" Run Script phase of the
// SketchAPI Xcode project so Xcode can rebuild the JS code only when relevant source files change.
class MSGenerateXCFileListPlugin {
constructor() {
this.inputFiles = new Set()
this.compilerCount = 0
this.shutdownCount = 0
}
apply(compiler) {
this.compilerCount++
compiler.hooks.done.tap(this.constructor.name, (stats) => {
const projectDir = compiler.context
for (const module of stats.compilation.modules) {
if (module.resource) {
this.inputFiles.add(module.resource.replace(projectDir, '$(SRCROOT)'))
}
}
})
compiler.hooks.shutdown.tap(this.constructor.name, () => {
this.shutdownCount++
if (this.shutdownCount < this.compilerCount) {
// Wait until all compilers have finished before writing the final .xcfilelist files
return
}
const header =
[
`# This file is auto-generated by ${this.constructor.name} in webpack.config.js`,
`# for the "Build SketchAPI" Run Script phase of the SketchAPI.xcodeproj.`,
`# Do not edit this file directly.`,
].join('\n') + '\n\n'
const sortedInputs = [...this.inputFiles].sort()
this.writeIfChanged(
path.resolve(__dirname, 'sketchapi-inputs.xcfilelist'),
header + sortedInputs.join('\n') + '\n'
)
})
}
writeIfChanged(filePath, content) {
const name = path.basename(filePath)
try {
const existing = fs.readFileSync(filePath, 'utf8')
if (existing === content) {
console.log(`[${this.constructor.name}] ${name} is up to date`)
return
}
} catch (e) {
// File doesn't exist yet, write it
}
fs.writeFileSync(filePath, content)
console.log(`[${this.constructor.name}] Updated ${name}`)
}
}
const xcfilelistplugin = new MSGenerateXCFileListPlugin()
const config = {
mode: PRODUCTION ? 'production' : 'development',
resolve: { extensions: ['.js', '.json'] },
module: {
rules: [babelLoader],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(PRODUCTION ? 'production' : 'development'),
API_VERSION: JSON.stringify(process.env.npm_package_version),
},
}),
xcfilelistplugin,
],
externals: [
(context, request, callback) => {
// core modules shipped in Sketch
if (CORE_MODULES.indexOf(request) !== -1) {
return callback(null, `commonjs ${request}`)
}
return callback()
},
],
}
module.exports = ENTRIES.map(({ entry, output }) => ({
...config,
...{
entry,
output: {
filename: output,
libraryTarget: 'commonjs2',
path: OUTPUT_PATH,
},
},
}))
module.exports.config = config
module.exports.CORE_MODULES = CORE_MODULES