Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Technical/issue 11 config refactor #43

Merged
merged 11 commits into from
Apr 22, 2019
6 changes: 5 additions & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ To develop for the project, you'll want to follow these steps:
Unit tests have been written that can be run using
```shell
$ yarn test
```
```

Note, you can use the following to adjust how many mocha tests get run:
- `describe.only` / `it.only`: only run this block
- `xdescribe` / `xit`: dont run this block
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"lint": "eslint \"./packages/**/**/*.js\" \"./test/**/**/*.js\"",
"build": "node ./packages/cli/index.js build",
"serve": "yarn clean && yarn build && cd ./public && ws",
"develop": "node ./packages/cli/index.js develop",
"test": "mocha --timeout 15000"
"develop": "yarn clean && node ./packages/cli/index.js develop",
"test": "yarn clean && mocha --timeout 15000"
},
"dependencies": {
"@babel/core": "^7.4.0",
Expand Down
161 changes: 79 additions & 82 deletions packages/cli/config/webpack.config.common.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');

const isDirectory = source => fs.lstatSync(source).isDirectory();
const getUserWorkspaceDirectories = (source) => {
return fs.readdirSync(source).map(name => path.join(source, name)).filter(isDirectory);
};

// TODO get userWorkspace and pagesDir from greenwood config?
// https://github.com/ProjectEvergreen/greenwood/issues/11
const userWorkspace = fs.existsSync(path.join(process.cwd(), 'src'))
? path.join(process.cwd(), 'src')
: path.join(__dirname, '..', 'templates/');

const pagesDir = fs.existsSync(path.join(process.cwd(), 'src', 'pages'))
? path.join(process.cwd(), 'src', 'pages/')
: path.join(__dirname, '..', 'templates/');

const mappedUserDirectoriesForWebpack = getUserWorkspaceDirectories(userWorkspace).map((userPath) => {
const mapUserWorkspaceDirectory = (userPath) => {
const directory = userPath.split('/')[userPath.split('/').length - 1];

return new webpack.NormalModuleReplacementPlugin(
Expand All @@ -32,80 +21,88 @@ const mappedUserDirectoriesForWebpack = getUserWorkspaceDirectories(userWorkspac
if (additionalNestedPathIndex > -1) {
resource.request = resource.request.substring(additionalNestedPathIndex + 2, resource.request.length);
}
});
});
}
);
};

module.exports = {
module.exports = (context) => {
// dynamically map all the user's workspace directories for resolution by webpack
// this essentially helps us keep watch over changes from the user, and greenwood's build pipeline
const mappedUserDirectoriesForWebpack = getUserWorkspaceDirectories(context.userWorkspace).map(mapUserWorkspaceDirectory);

entry: {
index: path.join(process.cwd(), '.greenwood', 'app', 'app.js')
},
return {

output: {
path: path.join(process.cwd(), 'public'),
filename: '[name].[hash].bundle.js',
publicPath: '/'
},
entry: {
index: path.join(context.scratchDir, 'app', 'app.js')
},

module: {
rules: [{
test: /\.js$/,
enforce: 'pre',
loader: 'eslint-loader',
options: {
configFile: path.join(__dirname, './.eslintrc')
}
}, {
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [
['babel-plugin-transform-builtin-classes', {
globals: ['LitElement']
}]
]
}
}, {
test: /\.md$/,
loaders: [
'babel-loader',
'wc-markdown-loader'
]
}, {
test: /\.css$/,
loaders: [
{ loader: 'css-to-string-loader' },
{ loader: 'css-loader' },
{ loader: 'postcss-loader', options:
{
config: {
path: path.join(__dirname)
}
}
output: {
path: context.publicDir,
filename: '[name].[hash].bundle.js',
publicPath: '/'
Copy link
Member

@hutchgrant hutchgrant Apr 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong place to hardcode this. Say I want site to run from mydomain.com/mysite/ as root path, well then '/' will lead to incorrect assets (such as manifest) etc. When I said this should be from the config, I meant the overall package config. You're calling it(what I was referring to as config) context now, assuming you'll add a separate config object, in addition to the context object, as part of the compilation object. You can see how I did this in my version of this PR

Certainly this should default to '/', but it should be configurable outside of webpack, by a user if they want. That's in addition to devServer var such host and port(Iwho knows, someone could have that port reserved or want to host on a network), etc. The issue was consolidating the config afterall.

All the other webpack configs are now relying on this publicPath(which is hardcoded).

I assume though you want to address this in #40 and leave it. Just be forewarned, when #40 is addressed, there will be things like this that will have to be modified as well.

Copy link
Member Author

@thescientist13 thescientist13 Apr 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it should be configurable outside of webpack,

for sure. please make an issue. configuration should be reviewed / implemented on a per item basis, so it would have to come after #40 .

},

module: {
rules: [{
test: /\.js$/,
enforce: 'pre',
loader: 'eslint-loader',
options: {
configFile: path.join(__dirname, './.eslintrc')
}
]
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
}, {
test: /\.(ttf|eot|svg|jpe?g|png|gif|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
}]
},
}, {
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [
['babel-plugin-transform-builtin-classes', {
globals: ['LitElement']
}]
]
}
}, {
test: /\.md$/,
loaders: [
'babel-loader',
'wc-markdown-loader'
]
}, {
test: /\.css$/,
loaders: [
{ loader: 'css-to-string-loader' },
{ loader: 'css-loader' },
{ loader: 'postcss-loader', options:
{
config: {
path: path.join(__dirname)
}
}
}
]
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
}, {
test: /\.(ttf|eot|svg|jpe?g|png|gif|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
}]
},

plugins: [
...mappedUserDirectoriesForWebpack,
plugins: [
...mappedUserDirectoriesForWebpack,

new webpack.NormalModuleReplacementPlugin(
/\.md/,
(resource) => {
resource.request = resource.request.replace(/^\.\//, pagesDir);
}),

new HtmlWebpackPlugin({
template: path.join(process.cwd(), '.greenwood', 'index.html'),
chunksSortMode: 'dependency'
})
]
new webpack.NormalModuleReplacementPlugin(
/\.md/,
(resource) => {
resource.request = resource.request.replace(/^\.\//, context.pagesDir);
}
),

new HtmlWebpackPlugin({
template: path.join(context.scratchDir, 'index.html'),
chunksSortMode: 'dependency'
})
]
};
};
115 changes: 57 additions & 58 deletions packages/cli/config/webpack.config.develop.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@
const fs = require('fs');
const path = require('path');
const commonConfig = require('./webpack.config.common');
const webpackMerge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const FilewatcherPlugin = require('filewatcher-webpack-plugin');
const generateBuild = require('../lib/generate');
const generateCompilation = require('../lib/compile');
const webpackMerge = require('webpack-merge');

const host = 'localhost';
const port = 1981;
const publicPath = commonConfig.publicPath;
let isRebuilding = false;

// TODO get userWorkspace and pagesDir from greenwood config?
// https://github.com/ProjectEvergreen/greenwood/issues/11

const userWorkspace = fs.existsSync(path.join(process.cwd(), 'src'))
? path.join(process.cwd(), 'src')
: path.join(__dirname, '..', 'templates/');

const rebuild = async() => {
if (!isRebuilding) {
isRebuilding = true;

// rebuild web components
await generateBuild();
await generateCompilation();

// debounce
setTimeout(() => {
isRebuilding = false;
}, 1000);
}
};

module.exports = webpackMerge(commonConfig, {

mode: 'development',
module.exports = (context) => {
const commonConfig = require(path.join(__dirname, '..', './config/webpack.config.common.js'))(context);
Copy link
Member

@hutchgrant hutchgrant Apr 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you're importing and initializing in one line, would it not be easier to import it at the top and then initialize it when it's called, within the merge? webpackMerge(commonConfig(context), {})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see what you mean! yeah, I like that. let me give it a shot.

const publicPath = commonConfig.output.publicPath;

return webpackMerge(commonConfig, {

mode: 'development',

entry: [
`webpack-dev-server/client?http://${host}:${port}`,
path.join(process.cwd(), '.greenwood', 'app', 'app.js')
],
entry: [
`webpack-dev-server/client?http://${host}:${port}`,
path.join(context.scratchDir, 'app', 'app.js')
],

devServer: {
port,
host,
historyApiFallback: true,
hot: false,
inline: true
},
devServer: {
port,
host,
historyApiFallback: true,
hot: false,
inline: true
},
Copy link
Member

@hutchgrant hutchgrant Apr 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables devServer: { port, host } should potentially be handled by config.


plugins: [
// new webpack.HotModuleReplacementPlugin(),
new FilewatcherPlugin({
watchFileRegex: [`/${userWorkspace}/`],
onReadyCallback: () => {
console.log(`Now serving Development Server available at http://${host}:${port}`);
},
// eslint-disable-next-line no-unused-vars
onChangeCallback: async (path) => {
rebuild();
},
usePolling: true,
atomic: true,
ignored: '/node_modules/'
}),
new ManifestPlugin({
fileName: 'manifest.json',
publicPath
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: '.greenwood/index.dev.html',
publicPath
}),
new HtmlWebpackPlugin({
filename: '404.html',
template: '.greenwood/404.dev.html',
publicPath
})
]
});
plugins: [
// new webpack.HotModuleReplacementPlugin(),
new FilewatcherPlugin({
watchFileRegex: [`/${context.userWorkspace}/`],
onReadyCallback: () => {
console.log(`Now serving Development Server available at http://${host}:${port}`);
},
// eslint-disable-next-line no-unused-vars
onChangeCallback: async () => {
rebuild();
},
usePolling: true,
atomic: true,
ignored: '/node_modules/'
}),
new ManifestPlugin({
fileName: 'manifest.json',
publicPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should use consolidated config's publicPath

}),
// TODO magic string paths (index.html)
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.join(context.scratchDir, 'index.dev.html'),
publicPath
}),
// TODO magic string paths (404.html)
new HtmlWebpackPlugin({
filename: '404.html',
template: path.join(context.scratchDir, '404.dev.html'),
publicPath
})
]
});
};
Loading