diff --git a/packages/core/src/build.js b/packages/core/src/build.js index 2ebf63a..6bd8d79 100644 --- a/packages/core/src/build.js +++ b/packages/core/src/build.js @@ -7,6 +7,7 @@ import { } from 'path'; import Latus from './latus'; +import R from './require'; const { LATUS_BABEL_CONFIG = require.resolve('@latus/build/build/.babelrc.js'), @@ -14,26 +15,24 @@ const { } = process.env; export default (latus) => (neutrino) => { - // eslint-disable-next-line no-eval - const r = eval('require'); - const airbnb = r('@neutrinojs/airbnb'); + const airbnb = R('@neutrinojs/airbnb'); airbnb({ eslint: { cache: false, - baseConfig: r(LATUS_ESLINT_DEFAULTS), + baseConfig: R(LATUS_ESLINT_DEFAULTS), }, })(neutrino); - const clean = r('@neutrinojs/clean'); + const clean = R('@neutrinojs/clean'); clean({ cleanOnceBeforeBuildPatterns: ['**/*.hot-update.*'], })(neutrino); - const {EnvironmentPlugin} = r('webpack'); + const {EnvironmentPlugin} = R('webpack'); neutrino.config .plugin('environment') .use(EnvironmentPlugin, [{ SIDE: 'server', }]); - const node = r('@neutrinojs/node'); + const node = R('@neutrinojs/node'); node({ babel: { configFile: LATUS_BABEL_CONFIG, @@ -44,7 +43,7 @@ export default (latus) => (neutrino) => { const config = {}; [plugin, `${plugin}/server`].forEach((path) => { try { - r.resolve(path); + R.resolve(path); config[path] = {}; } // eslint-disable-next-line no-empty @@ -65,7 +64,7 @@ export default (latus) => (neutrino) => { .rules.store.get('compile') .uses.store.get('babel') .store.get('options'); - r('@babel/register')({ + R('@babel/register')({ plugins: babelOptions.plugins, presets: babelOptions.presets, }); @@ -87,7 +86,7 @@ export default (latus) => (neutrino) => { }); source.push('}'); // Everything's a simulation... - const virtual = r.resolve(`${__dirname}/virtual`); + const virtual = R.resolve(`${__dirname}/virtual`); neutrino.config .entry('index') .add(virtual); @@ -102,12 +101,12 @@ export default (latus) => (neutrino) => { neutrino.config .entry('index') .add('@latus/core/start'); - const mocha = r('@neutrinojs/mocha'); + const mocha = R('@neutrinojs/mocha'); mocha()(neutrino); if (process.env.LATUS_LINTING) { return; } - const nodeExternals = r('webpack-node-externals'); + const nodeExternals = R('webpack-node-externals'); const allowlist = [ /^@latus\/core\/virtual$/, ]; @@ -117,7 +116,7 @@ export default (latus) => (neutrino) => { .prepend('dotenv/config'); allowlist.push(/^webpack/); paths.forEach((path) => { - const resolved = resolve(r.resolve(path)); + const resolved = resolve(R.resolve(path)); // Cheating for now... const parts = resolved.split('/'); parts.splice(parts.indexOf('packages') + 2, 0, 'src'); @@ -134,8 +133,8 @@ export default (latus) => (neutrino) => { allowlist, })); const entries = neutrino.config.entryPoints.store.get('index').store; - if (entries.has(`${r.resolve('webpack/hot/poll')}?1000`)) { - entries.delete(`${r.resolve('webpack/hot/poll')}?1000`); + if (entries.has(`${R.resolve('webpack/hot/poll')}?1000`)) { + entries.delete(`${R.resolve('webpack/hot/poll')}?1000`); const entriesArray = Array.from(entries); entriesArray.unshift('webpack/hot/signal'); // eslint-disable-next-line no-param-reassign diff --git a/packages/core/src/gather.js b/packages/core/src/gather.js index 15cd13e..c4550fa 100644 --- a/packages/core/src/gather.js +++ b/packages/core/src/gather.js @@ -67,22 +67,20 @@ export default ( .sort(([lname], [rname]) => (lname < rname ? -1 : 1)) .map(([type, Class]) => { const thisUid = uid++; - // TODO: I think we can do computed class members now. - const source = ` - (${type}) => class Gathered${type} extends ${type} { - - static get ${idAttribute}() { - return ${thisUid}; - } - - static get ${typeAttribute}() { - return '${type}'; - } + class Subclass extends Class { + static get [idAttribute]() { + return thisUid; } - `; - // eslint-disable-next-line no-eval - const Subclass = eval(source)(Class); + + static get [typeAttribute]() { + return type; + } + + } + if (!Subclass.name) { + Subclass.name = type; + } fromId[thisUid] = Subclass; return [ type, diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 6fd4ba9..7d7502b 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -11,6 +11,7 @@ export {compose, fastApply} from './function'; export {decorateWithLatus, gatherWithLatus, default as gather} from './gather'; export {default as Middleware} from './middleware'; export {default as Latus} from './latus'; +export {default as require} from './require'; export * from './string'; export const Class = class {}; diff --git a/packages/core/src/latus.js b/packages/core/src/latus.js index 1ca578b..cf123e5 100644 --- a/packages/core/src/latus.js +++ b/packages/core/src/latus.js @@ -8,6 +8,7 @@ import without from 'lodash.without'; import Middleware from './middleware'; import readConfig from './read-config'; +import R from './require'; const debug = D('@latus/core/latus'); @@ -56,8 +57,6 @@ export default class Latus { } static create(config = readConfig()) { - // eslint-disable-next-line no-eval - const R = eval('require'); const paths = flatten( Object.keys(config).map((path) => [ this.runtimePath(path), @@ -211,7 +210,6 @@ export default class Latus { } static mock(modules, config = {}) { - // eslint-disable-next-line no-eval return new Latus({ config: { ...Object.fromEntries(Object.keys(modules).map((path) => [path, {}])), @@ -222,8 +220,6 @@ export default class Latus { } static runtimePath(path) { - // eslint-disable-next-line no-eval - const R = eval('require'); try { const local = join(process.cwd(), 'src', path); R.resolve(local); diff --git a/packages/core/src/require.js b/packages/core/src/require.js new file mode 100644 index 0000000..af7a7d5 --- /dev/null +++ b/packages/core/src/require.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line no-eval +export default eval('"undefined" !== typeof require ? require : () => {}'); diff --git a/packages/http/src/build/client-plugins.js b/packages/http/src/build/client-plugins.js index 4976fcd..1c2eb58 100644 --- a/packages/http/src/build/client-plugins.js +++ b/packages/http/src/build/client-plugins.js @@ -1,5 +1,7 @@ const uniq = require('lodash.uniq'); +const {require: R} = require('@latus/core'); + module.exports = async (latus, req) => { const plugins = await latus.invokeReduceAsync( '@latus/http/plugins', @@ -19,8 +21,7 @@ module.exports = async (latus, req) => { } [plugin, `${plugin}/client`].forEach((path) => { try { - // eslint-disable-next-line no-eval - eval('require.resolve')(path); + R.resolve(path); config[path] = {}; } // eslint-disable-next-line no-empty diff --git a/packages/http/src/build/neutrino/targets.js b/packages/http/src/build/neutrino/targets.js index 9f9a431..8964e3f 100644 --- a/packages/http/src/build/neutrino/targets.js +++ b/packages/http/src/build/neutrino/targets.js @@ -2,12 +2,11 @@ const {isAbsolute, join} = require('path'); const fs = require('fs'); +const {require: R} = require('@latus/core'); const {DefinePlugin} = require('webpack'); module.exports = () => (neutrino) => { - // eslint-disable-next-line no-eval - const r = eval('require'); - const copy = r('@neutrinojs/copy'); + const copy = R('@neutrinojs/copy'); copy({ patterns: [{ from: join(neutrino.options.root, 'src', 'assets'), diff --git a/packages/http/src/build/neutrino/virtual.js b/packages/http/src/build/neutrino/virtual.js index fabae15..acf66d2 100644 --- a/packages/http/src/build/neutrino/virtual.js +++ b/packages/http/src/build/neutrino/virtual.js @@ -1,17 +1,15 @@ -const {Latus} = require('@latus/core'); +const {Latus, require: R} = require('@latus/core'); const clientPlugins = require('../client-plugins'); module.exports = async (latus) => { const config = await clientPlugins(latus); return (neutrino) => { - // eslint-disable-next-line no-eval - const r = eval('require'); const babelOptions = neutrino.config.module .rules.store.get('compile') .uses.store.get('babel') .store.get('options'); - r('@babel/register')({ + R('@babel/register')({ plugins: babelOptions.plugins, presets: babelOptions.presets, }); @@ -22,7 +20,7 @@ module.exports = async (latus) => { .map((path) => Latus.runtimePath(`${path}/index.css.js`)) .filter((path) => !!path); const cssAppliers = cssPaths.map((path) => `require('${path}')`).join(';'); - const virtual = r.resolve('@latus/http/virtual'); + const virtual = R.resolve('@latus/http/virtual'); const source = [ `${cssAppliers}`, 'export default {', @@ -49,7 +47,7 @@ module.exports = async (latus) => { const testPaths = paths .map((path) => [path, Latus.runtimePath(`${path}/test`)]) .filter(([, path]) => !!path); - const tests = r.resolve('@latus/http/tests'); + const tests = R.resolve('@latus/http/tests'); neutrino.config.module .rule(tests) .test(tests) diff --git a/packages/http/src/http.js b/packages/http/src/http.js index 6468885..19966e4 100644 --- a/packages/http/src/http.js +++ b/packages/http/src/http.js @@ -2,7 +2,7 @@ import {createReadStream} from 'fs'; import {createServer, ServerResponse} from 'http'; import {dirname, join} from 'path'; -import {arrayFlatten, Latus} from '@latus/core'; +import {arrayFlatten, Latus, require as R} from '@latus/core'; import express from 'express'; // eslint-disable-next-line import/no-extraneous-dependencies import httpProxy from 'http-proxy'; @@ -49,8 +49,7 @@ export const createHttpServer = async (latus) => { .map((path) => Latus.runtimePath(path)) .filter((path) => !!Latus.runtimePath(`${path}/test`)) .map((path) => { - // eslint-disable-next-line no-eval - const staticPath = dirname(eval('require').resolve(path)); + const staticPath = dirname(R.resolve(path)); return express.static(staticPath, {redirect: false}); }); for (let i = 0; i < middlewares.length; i++) { diff --git a/packages/http/src/server.js b/packages/http/src/server.js index 26ebbb3..d840875 100644 --- a/packages/http/src/server.js +++ b/packages/http/src/server.js @@ -1,5 +1,7 @@ import {spawn} from 'child_process'; +import {require as R} from '@latus/core'; + import {createHttpServer} from './http'; const { @@ -23,8 +25,8 @@ export default { !process.argv.find((arg) => '--watch' === arg) || process.argv.find((arg) => 'production' === arg) ) { - // eslint-disable-next-line global-require, no-eval, no-param-reassign - configs.http = eval('require')('./build/.neutrinorc'); + // eslint-disable-next-line no-param-reassign + configs.http = R('./build/.neutrinorc'); } else if (-1 === excludeBuilds.indexOf('http')) { const binary = `$(npm --prefix ${process.cwd()} bin)/webpack-dev-server`; diff --git a/packages/react/package.json b/packages/react/package.json index 79fa0f9..6996c31 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -24,6 +24,7 @@ ], "dependencies": { "@hot-loader/react-dom": "^17.0.1", + "@latus/core": "^2.0.0", "@neutrinojs/react": "^9.4.0", "autoprefixer": "^9.8.6", "debug": "4.3.1", diff --git a/packages/react/src/build/react.js b/packages/react/src/build/react.js index c660af7..7bce85a 100644 --- a/packages/react/src/build/react.js +++ b/packages/react/src/build/react.js @@ -1,12 +1,12 @@ const {dirname} = require('path'); +const {require: R} = require('@latus/core'); const react = require('@neutrinojs/react'); -// eslint-disable-next-line no-eval -const rr = eval('require').resolve; +const {resolve} = R; const { - LATUS_BABEL_CONFIG = rr('@latus/build/build/.babelrc.js'), - LATUS_POSTCSS_CONFIG = rr('@latus/build/build/postcss.config.js'), + LATUS_BABEL_CONFIG = resolve('@latus/build/build/.babelrc.js'), + LATUS_POSTCSS_CONFIG = resolve('@latus/build/build/postcss.config.js'), } = process.env; module.exports = () => (neutrino) => { diff --git a/packages/react/yarn.lock b/packages/react/yarn.lock index 4e0c013..80692d6 100644 --- a/packages/react/yarn.lock +++ b/packages/react/yarn.lock @@ -932,6 +932,20 @@ webpack "^4" webpack-cli "^3" +"@latus/core@^2.0.0": + version "2.0.0" + resolved "http://npm.cha0sdev/@latus%2fcore/-/core-2.0.0.tgz#12a0f3c11c9832a60e6726ea74ce0ae9b45e9faf" + integrity sha512-JMxgl8Qsvby2OKtzXEs7cjamvsgVxEbJXbjebsmDl+J9Cq5513/4Xpq37EYYvT6FEZpoHFoIkQsaG+89JQzTwg== + dependencies: + autoprefixer "^9.8.6" + debug "4.3.1" + js-yaml "3.14.0" + lodash.flatten "^4.4.0" + lodash.get "^4.4.2" + lodash.set "^4.3.2" + mkdirp "^1.0.4" + webpack-virtual-modules "^0.4.1" + "@neutrinojs/airbnb@^9.4.0": version "9.5.0" resolved "http://npm.cha0sdev/@neutrinojs%2fairbnb/-/airbnb-9.5.0.tgz#87cfc09c4237a2da632c80d7b91ad447a5e0b3f2" @@ -4254,6 +4268,14 @@ js-base64@^2.1.8: resolved "http://npm.cha0sdev/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ== +js-yaml@3.14.0: + version "3.14.0" + resolved "http://npm.cha0sdev/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482" + integrity sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A== + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + js-yaml@4.0.0: version "4.0.0" resolved "http://npm.cha0sdev/js-yaml/-/js-yaml-4.0.0.tgz#f426bc0ff4b4051926cd588c71113183409a121f" @@ -4481,11 +4503,26 @@ lodash.clonedeep@^4.5.0: resolved "http://npm.cha0sdev/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" integrity sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8= +lodash.flatten@^4.4.0: + version "4.4.0" + resolved "http://npm.cha0sdev/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f" + integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8= + +lodash.get@^4.4.2: + version "4.4.2" + resolved "http://npm.cha0sdev/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99" + integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk= + lodash.omit@^4.5.0: version "4.5.0" resolved "http://npm.cha0sdev/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60" integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA= +lodash.set@^4.3.2: + version "4.3.2" + resolved "http://npm.cha0sdev/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" + integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM= + lodash@^4.0.0, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@~4.17.10: version "4.17.20" resolved "http://npm.cha0sdev/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" @@ -4741,7 +4778,7 @@ mkdirp@^0.5.1, mkdirp@^0.5.3: dependencies: minimist "^1.2.5" -mkdirp@^1.0.3: +mkdirp@^1.0.3, mkdirp@^1.0.4: version "1.0.4" resolved "http://npm.cha0sdev/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== @@ -7050,6 +7087,11 @@ webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-sources@^1.4.1: source-list-map "^2.0.0" source-map "~0.6.1" +webpack-virtual-modules@^0.4.1: + version "0.4.2" + resolved "http://npm.cha0sdev/webpack-virtual-modules/-/webpack-virtual-modules-0.4.2.tgz#68ce4479df7334a491b7a3f3bead47fe382947d9" + integrity sha512-OUsT1VZhArN8nY7g6mMlw91HWnXcNXsIQjsQ83WteF4ViZ6YXqF2sWKOTDIZ0H+PPiApQdszLdZIrD7NNlU0Yw== + webpack@^4: version "4.46.0" resolved "http://npm.cha0sdev/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542"