UNPKG

2.54 kBJavaScriptView Raw
1const path = require('path')
2const fs = require('fs')
3const webpack = require('webpack')
4
5process.env.NODE_ENV = 'production'
6
7const LINC_DIR = path.resolve(__dirname, '..')
8const PROJECT_DIR = process.cwd()
9const MODULES_DIR = path.resolve(PROJECT_DIR, 'node_modules')
10
11const packageJson = require(path.resolve(PROJECT_DIR, 'package.json'))
12const lincConfig = packageJson.linc || {}
13const src = lincConfig.sourceDir || 'src'
14const srcDir = path.resolve(PROJECT_DIR, src)
15
16const deps = []
17if (packageJson.dependencies) {
18 deps.concat(Object.keys(packageJson.dependencies))
19}
20if (packageJson.devDependencies) {
21 deps.concat(Object.keys(packageJson.devDependencies))
22}
23
24function ensureSlash(path, needsSlash) {
25 var hasSlash = path.endsWith('/')
26 if (hasSlash && !needsSlash) {
27 return path.substr(path, path.length - 1)
28 } else if (!hasSlash && needsSlash) {
29 return path + '/'
30 } else {
31 return path
32 }
33}
34
35// We use "homepage" field to infer "public path" at which the app is served.
36// Webpack needs to know it to put the right <script> hrefs into HTML even in
37// single-page apps that may serve index.html for nested URLs like /todos/42.
38// We can't use a relative path in HTML because we don't want to load something
39// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
40var homepagePath = packageJson.homepage
41var homepagePathname = homepagePath ? url.parse(homepagePath).pathname : '/'
42// Webpack uses `publicPath` to determine where the app is being served from.
43// It requires a trailing slash, or the file assets will get an incorrect path.
44var publicPath = ensureSlash(homepagePathname, true)
45// `publicUrl` is just like `publicPath`, but we will provide it to our app
46// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
47// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
48var publicUrl = ensureSlash(homepagePathname, false)
49
50const env = lincConfig.build_env || {}
51const defineEnv = {}
52Object.keys(env).forEach(key => (defineEnv[key] = JSON.stringify(env[key])))
53defineEnv['process.env.NODE_ENV'] = JSON.stringify('production')
54const definePlugin = new webpack.DefinePlugin(defineEnv)
55
56const createPlugin = options => {
57 let Plugin = require(path.resolve(MODULES_DIR, options.import))
58 if (options.exportName) {
59 Plugin = Plugin[options.exportName]
60 }
61 return new Plugin(options.options)
62}
63
64module.exports = {
65 LINC_DIR,
66 PROJECT_DIR,
67 packageJson,
68 lincConfig,
69 srcDir,
70 deps,
71 definePlugin,
72 publicPath,
73 createPlugin
74}